﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Web;

namespace DX_DailyUtil_1._0._1.Web.App_Ctrl.logic
{
    public class DB_Logic_Query : DB_Logic_Base
    {

        private String mTableName;

        private List<In_Where> mWhere = new List<In_Where>();

        private List<In_Select> mSelect = new List<In_Select>();

        public String MTableName
        {
            get { return mTableName; }
            set { mTableName = value; }
        }

        public List<In_Where> MWhere
        {
            get { return mWhere; }
            set { mWhere = value; }
        }

        public List<In_Select> MSelect
        {
            get { return mSelect; }
            set { mSelect = value; }
        }

        private Dictionary<String, Object> condition = new Dictionary<String, Object>();

        public Dictionary<String, Object> Condition
        {
            get { return condition; }
            set { condition = value; }
        }

        private String logic = "and";

        public String Logic
        {
            get { return logic; }
            set { logic = value; }
        }

        public DB_Logic_Query()
        {
        }

        public bool check<T>(T aObjt)
        {
            bool _result = false;
            int _logic_count = 0;
            int _condition_count = this.condition.Count;
            foreach (var item in this.condition)
            {
                var _key = aObjt.GetType().GetField(item.Key).GetValue(aObjt);
                if (item.Value.Equals(_key))
                {
                    _logic_count++;
                }
            }
            if ("and".Equals(this.logic))
            {
                // 全部都OK才OK
                _result = _logic_count.Equals(_condition_count) ? true : false;
            }
            else if ("or".Equals(this.logic))
            {
                // 一个OK就OK
                _result = _logic_count > 1 ? true : false;
            }

            return _result;

        }

        [Serializable]
        public class In_Where
        {
            public String[] field;
            public String logic = "Equal";
            public Object value;
            public bool default_condition = true;

        }

        [Serializable]
        public class In_Select
        {
            public String[] field;
        }
    }
}